﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SSActionManager : MonoBehaviour {
    private HashSet<SSAction> actions = new HashSet<SSAction>();
    private List<SSAction> waitingAdd = new List<SSAction>();
    private List<SSAction> waitingDelete = new List<SSAction>();
    bool waitingClear = false;

    void Start() {
        
    }
    protected void Update() {
        if (waitingClear) {
            waitingClear = false;
            actions.Clear();
            waitingAdd.Clear();
            waitingDelete.Clear();
            return;
        }
        foreach (SSAction ac in waitingAdd) {
            actions.Add(ac);
        }
        waitingAdd.Clear();
        
        foreach (SSAction ac in actions) {
            if (ac.IsEnable()) {
                ac.Update();
                if (ac.IsOver()) {
                    waitingDelete.Add(ac);
                }
            }
        }

        foreach (SSAction ac in waitingDelete) {
            actions.Remove(ac);
            Destroy(ac);
        }
        waitingDelete.Clear();
    }

    public void RunAction(SSAction action) {
        waitingAdd.Add(action);
    }

    public void Clear() {
        waitingClear = true;
    }
}
